declare const window: any
declare const process: any

import { App } from 'vue'
import androidVue from '@/server/android-vue';
import { Notify, Toast, ToastOptions, ToastType, type NotifyOptions } from 'vant';

import {
  PageFrame,
  PageFooter,
  PageTab,
  PageSearch,
  PageLineText,
  AppIframe,
  Chart
} from '@/importComponents'
import { ToastOptions, ToastType } from 'vant';

const install = function (app: App, options: any = {}) {
  // 注册全局组件
  app.component('page-frame', PageFrame)

  app.component('page-footer', PageFooter)
  app.component('page-tab', PageTab)
  app.component('page-search', PageSearch)
  app.component('page-line-text', PageLineText)
  app.component('app-iframe', AppIframe)
  app.component('chart', Chart)


  /**
   * 添加全局的 后退事件
   */
  app.config.globalProperties.goBack = function (isBackReload: Boolean) {
    if (isBackReload) {
      setTimeout(() => {
        window.location.reload()
      }, 300)
    }
    window.history.go(-1)
  }


  const $native = new androidVue.NativeJS();
  app.config.globalProperties.$native = $native;
  app.config.globalProperties.cancelMessageReminder = function () {
    try {
      $native.cancelPromptSound();
      setTimeout(() => {
        $native.cancelVibrator();
      }, 500);
    } catch (error) { }
  };

  /**
   * 注册全局强提醒时间
   */
  app.config.globalProperties.notifyMessageReminder = function (title: string, text: string) {
    const isDevelopment = (process as any).env.NODE_ENV === 'development'
    // if (isDevelopment) {
    //   return;
    // }
    if (window.notifyTimeoutId > 0) {
      clearTimeout(window.notifyTimeoutId);
    }
    window.notifyTimeoutId = setTimeout(() => {
      try {
        $native.showNotify(title, text);
        let url = "http://vsuzeqbigdata/xinwenlianbo.MP3";
        try {
          if (!isDevelopment) {
            url = window._APK_UPDATE_CONFIG_INFO[APP_CONFIG_KEY].voicePath
          } else {
            url = 'https://www.runoob.com/try/demo_source/horse.mp3';
          }

        } catch (e) { }
        $native.playPromptSound(url);
        setTimeout(() => {
          $native.playVibrator(
            true,
            [
              1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
              1000, 1000, 1000, 1000, 1000, 500, 500,
            ]
          );
        }, 400);
      } catch (e1) {

      }
    }, 1000);
  };



  const toast = function (options: ToastOptions | string, type?: ToastType) {
    if (typeof options == 'string') {
      Toast({ message: options, type: type });
      return;
    }
    Toast(options);
  }

  /**
   * 兼容 element-plus中的$message
   */
  app.config.globalProperties.$message = {
    // error: (option: ToastOptions | string) => toast(option, 'fail'),
    error: (message: string) => {
      const options = {
        type: 'danger', message
      } as NotifyOptions;

      Notify(options)
    },
    success: (option: ToastOptions | string) => toast(option, 'success'),
    warning: (option: ToastOptions | string) => toast(option, 'fail'),
    info: (option: ToastOptions | string) => toast(option, 'text'),
    html: (option: ToastOptions | string) => toast(option, 'html'),
    notify: (options: number | string | NotifyOptions) => {
      Notify(options)
    },
    errorNotify: (message: string) => {
      const options = {
        type: 'danger', message
      } as NotifyOptions;

      Notify(options)
    }
  }
}
/* istanbul ignore if */
if (typeof window !== 'undefined' && window.Vue) {
  install(window.Vue)
}

export default {
  install
}
